package com.ymatou.iapi.shoppingcart.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcart.parameter.ClearInvalidProductBean;
import com.ymatou.iapi.shoppingcart.service.ClearInvalidProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerAppShoppingCartService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_ClearInvalidProduct {
	
	private static ClearInvalidProductBean clearInvalidProductBean;
	private static ClearInvalidProductCall clearInvalidProductCall;
	
	private static int buyerUserId = 20226821;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("清除购物车失效商品");
	}

	@Before
	public void caseUp() throws Exception {
		clearInvalidProductBean = new ClearInvalidProductBean();
		clearInvalidProductCall = new ClearInvalidProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(clearInvalidProductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_ClearInvalidProduct_001() {
		 Logger.start(true,"验证清空单个失效商品，mongodb数据是否清空");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 BuyerAppShoppingCartService buyerAppShoppingCartService = new BuyerAppShoppingCartService();
			 buyerAppShoppingCartService.addShoppingCart(productId, buyerUserId, 1);
			 
			 SellerProductCallService.batchSetProductOffSale(productId, sellerId);
			 
			 buyerAppShoppingCartService.getShoppingCart(buyerUserId);
			 
			 clearInvalidProductBean.setUserId(buyerUserId);
			 
			 clearInvalidProductCall.setData(clearInvalidProductBean);
			 clearInvalidProductCall.callService();
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 int size = 2;
			 if (null == wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', " + "'userid':" + buyerUserId + "}"))
				 size = 0;
			 else 
				 size = wapper.getOneShoppingCartInfoForMap("{'productid':'" + productId + "', " + "'userid':" + buyerUserId + "}").size();
			 
			 Logger.comment("clearInvalidProductCall send:" + clearInvalidProductCall.getEntityString());
			 Logger.comment("clearInvalidProductCall response:" + clearInvalidProductCall.getReturnData());
			 
			 Logger.verifyEquals(0, size, "验证mongodb数据是否清空");		
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_ClearInvalidProduct_002() {
		 Logger.start(true,"验证清空多个失效商品，mongodb数据是否清空");
		 try {
			 String productId1 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 String productId2 = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 BuyerAppShoppingCartService buyerAppShoppingCartService = new BuyerAppShoppingCartService();
			 buyerAppShoppingCartService.addShoppingCart(productId1, buyerUserId, 1);
			 buyerAppShoppingCartService.addShoppingCart(productId2, buyerUserId, 1);
			 
			 SellerProductCallService.batchSetProductOffSale(productId1, sellerId);
			 SellerProductCallService.batchSetProductOffSale(productId2, sellerId);
			 
			 buyerAppShoppingCartService.getShoppingCart(buyerUserId);
			 
			 clearInvalidProductBean.setUserId(buyerUserId);
			 
			 clearInvalidProductCall.setData(clearInvalidProductBean);
			 clearInvalidProductCall.callService();
			 
			 ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
			 
			 int size = 2;
			 if (null == wapper.getOneShoppingCartInfoForMap("{'userid':" + buyerUserId + "}"))
				 size = 0;
			 else 
				 size = wapper.getOneShoppingCartInfoForMap("{'userid':" + buyerUserId + "}").size();
			 
			 Logger.comment("clearInvalidProductCall send:" + clearInvalidProductCall.getEntityString());
			 Logger.comment("clearInvalidProductCall response:" + clearInvalidProductCall.getReturnData());
			 
			 Logger.verifyEquals(0, size, "验证mongodb数据是否清空");		
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
}
